﻿
@{
    ViewBag.Title = "补片管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var fragments = ViewBag.FragmentTypes as List<KeyValuePair<int, string>>;
    var processTypes = ViewBag.CompenProcessTypes as List<KeyValuePair<int, string>>;
    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var workOrderNos = ViewBag.WorkOrderNos as List<string>;
    var startTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddDays(-1).AddHours(20) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(8) : DateTime.Today.AddHours(20));
    var endTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddHours(8) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(20) : DateTime.Today.AddDays(1).AddHours(8));
}
@model IOA.MES.DataAccess.VModel.VWorkOrder

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">生产车间：</label>
                        <div class="layui-input-inline">
                            <select name="WorkshopCode" lay-filter="WorkshopCode">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号：</label>
                        <div class="layui-input-inline">
                            <select name="WorkOrderNo" lay-search lay-filter="WorkOrderNo">
                                <option value="">请选择</option>
                                @foreach (var item in workOrderNos)
                                {
                                    <option value="@item">@item</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CompenProcess">补片工序：</label>
                        <div class="layui-input-inline">
                            <select name="CompenProcess" lay-filter="CompenProcess">
                                <option value="">全部工序</option>
                                @foreach (var item in processTypes)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CompenReason">补片原因：</label>
                        <div class="layui-input-inline">
                            <select name="CompenReason" lay-filter="CompenReason">
                                <option value="">全部原因</option>
                                @foreach (var item in fragments)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OperateTime">操作时间：</label>
                        <div class="layui-input-inline" style="width: 310px;">
                            <input type="text" id="txtRangeTime" name="TimeRange" style="width:310px;float:left;" value="@(startTime.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + endTime.ToString("yyyy-MM-dd HH:mm:ss"))" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-list" lay-filter="submit" lay-submit style="margin-top:-21px;">
                            <i class="layui-icon layui-icon-ok layuiadmin-button-btn"></i>查询
                        </button>
                        <button class="layui-btn layui-btn-normal layuiadmin-btn-list" id="btnAdd" style="margin-top:-21px;">新建补片</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeTime'
                    , type: 'datetime'
                    , rigger: 'click'
                    , range: '至'
                });
                form.on("select(WorkshopCode)", function (data) {
                    var loadId = layer.load();
                    $.ajax({
                        url: "/WorkOrder/QueryWorkshopWorkOrderNos",
                        data: {
                            workshopCode: data.value
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            var html = ["<option value=''>请选择</option>"];
                            for (var i = 0; i < res.Data.length; i++) {
                                html.push("<option value='" + res.Data[i] + "'>" + res.Data[i] + "</option>");
                            }
                            $("select[name=WorkOrderNo]").html(html.join("\n"));
                            form.render();
                        }
                    });
                });

                var options = {
                    elem: '#table'
                    , url: '/WorkOrder/QueryCompenCellRecords'
                    , method: "post"
                    , where: { workshopCode: $("select[name=WorkshopCode]").val().trim(), workOrderNo: "", process: null, reason: null, startTime: "@startTime.ToString("yyyy-MM-dd HH:mm:ss")", endTime: "@endTime.ToString("yyyy-MM-dd HH:mm:ss")" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60, fixed: "left" }
                        , { field: 'OrderNo', title: '订单号', minWidth: 150, fixed: "left" }
                        , { field: 'WorkOrderNo', title: '工单号', minWidth: 150, fixed: "left" }
                        , { field: 'CellCode', title: '电池料号', minWidth: 200 }
                        , { field: 'CellBatchNo', title: '批次号', minWidth: 200 }
                        , { field: 'WorkshopName', title: '车间', minWidth: 100 }
                        , {
                            title: '补片时间', minWidth: 280, templet: function (d) {
                                return d.StartTimeStr + " 至 " + d.EndTimeStr;
                            }
                        }
                        , { field: 'CompenDutyName', title: '班次', minWidth: 80 }
                        , { field: 'CnCompenProcess', title: '工序', minWidth: 100 }
                        , { field: 'CnCompenReason', title: '原因', minWidth: 100 }
                        , { field: 'CellCount', title: '数量', minWidth: 100 }
                        , { field: 'CNCreateBy', title: '操作人', minWidth: 100 }
                        , { field: 'CreateTimeStr', title: '操作时间', minWidth: 150 }
                    ]]
                };

                form.on("submit(submit)", function (data) {
                    options.where.workshopCode = data.field.WorkshopCode;
                    options.where.workOrderNo = data.field.WorkOrderNo;
                    options.where.process = data.field.CompenProcess == "" ? null : parseInt(data.field.CompenProcess);
                    options.where.reason = data.field.CompenReason == "" ? null : parseInt(data.field.CompenReason);
                    options.where.startTime = data.field.TimeRange.split('至')[0].trim();
                    options.where.endTime = data.field.TimeRange.split('至')[1].trim();
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }

                $("#btnAdd").click(function () {
                    parent.layui.index.openTabsPage("/WorkOrder/CompenCell", "补片");
                })
            });
        }
    </script>
}
